let token = JSON.parse(localStorage.getItem('token'))

//取出分类列表数据，初始化列表

function render() {
    getAticList(token, function(data) {
            $('.layui-table tbody').html('')
            data.data.forEach(value => {
                let temp = `
            <tr>
            <td>${value.name}</td>
            <td>${value.alias}</td>
            <td data-id="${value.Id}">
                <button type="button" class="layui-btn layui-btn-sm edite">编辑</button>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger">删除</button>
            </td>
            </tr>
            `
                $('.layui-table tbody').append(temp)
            });
        },
        function(data) {
            layer.msg('列表获取失败')
        })
}

render()

//添加文章类别

//添加插件中的弹出框
$('#addList').click(function() {
    let index = layer.open({
        title: '添加文章分类',
        type: 1,
        area: ['500px', '260px'],
        content: $('#layer-box')
    });
    layui.form.on('submit(subadd)', function(data) {
        let newal = data.field
        addAtic(token, newal, function(data) {
            layer.msg(data.message)
            $('#reset').trigger('click')
                // $('.layui-layer-close1').trigger('click')
            layer.close(index)
            render()
        }, function(data) {
            layer.msg(data.message)
        })
        return false
    })
})


//删除功能

$('.layui-table tbody').on('click', '.layui-btn-danger', function() {
    let index = $(this).parent().attr('data-id')
    delAtic(token, index, function(data) {
        layer.msg(data.message)
        render()
    })
})

//编辑功能
$('.layui-table tbody').on('click', '.edite', function() {
    let alias = $(this).parent().prev().html()
    let name = $(this).parent().prev().prev().html()
    $('#layer-box-2 .name').val(name)
    $('#layer-box-2 .alias').val(alias)
    let id = $(this).parent().attr('data-id')
    let index = layer.open({
        title: '编辑文章分类',
        type: 1,
        area: ['500px', '260px'],
        content: $('#layer-box-2')
    });
    layui.form.on('submit(sub)', function(data) {
        //修改数据的对象
        let da = data.field
        da.Id = id
        updateAtic(token, da, function(data) {
            layer.close(index)
            layer.msg(data.message)
            render()
        })
        return false
    })
})